package com.tepth.remote.control.manager;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;

import com.tepth.remote.control.R;
import com.tepth.remote.control.base.bean.NotificationBean;
import com.tepth.remote.core.app.Control;

/**
 * Description:
 *
 * @author Hequn.Lee
 * @date 2018/11/9
 */
public class ControlNotificationManager {

    /**
     * 通知管理器
     */
    private final NotificationManager mNotificationManager;

    private final String notificationId = "tepth_control_channel";

    public static final String NOTIFICATION_NAME = "notification";

    private ControlNotificationManager() {
        mNotificationManager = (NotificationManager) Control.getApplicationContext()
                .getSystemService(Context.NOTIFICATION_SERVICE);
    }

    private static final class Holder {
        private static final ControlNotificationManager INSTANCE = new ControlNotificationManager();
    }

    public static ControlNotificationManager getInstance() {
        return Holder.INSTANCE;
    }

    /**
     * 针对8.0以上的系统设置通知渠道
     *
     * @return 返回通知管理器
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public ControlNotificationManager init() {
        //判断是否创建过通知渠道
        if (mNotificationManager.getNotificationChannel(notificationId) != null) {
            return this;
        }
        // 用户可以看到的通知渠道的名字.
        CharSequence name = Control.getApplicationContext().getString(R.string.channel_name);
        // 用户可以看到的通知渠道的描述
        String description = Control.getApplicationContext().getString(R.string.channel_description);
        final NotificationChannel mChannel = new NotificationChannel(notificationId, name, NotificationManager.IMPORTANCE_HIGH);
        // 配置通知渠道的属性
        mChannel.setDescription(description);
        // 设置通知出现时的闪灯（如果 android 设备支持的话）
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        //设置提示音和弹框通知
        mChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
        // 设置通知出现时的震动（如果 android 设备支持的话）
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        //最后在NotificationManager中创建该通知渠道
        mNotificationManager.createNotificationChannel(mChannel);
        return this;
    }

    public NotificationManager getNotificationManager() {
        return mNotificationManager;
    }

    /**
     * 创建通知
     *
     * @param title 通知标题
     * @param text  通知内容
     * @return 通知对象
     */
    public Notification createPermanentNotification(String title, String text) {
        Notification notification;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            notification = new Notification
                    .Builder(Control.getApplicationContext(),
                    notificationId)
                    .setContentTitle(title)
                    .setContentText(text)
                    .setAutoCancel(false)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
        } else {
            notification = new Notification.Builder(Control.getApplicationContext())
                    .setContentTitle(title)
                    .setContentText(text)
                    .setAutoCancel(false)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
        }
        // 在通知栏常驻
        notification.flags = Notification.FLAG_ONGOING_EVENT;
        return notification;
    }

    public Notification createPermanentNotification(String title, String text, String className) {
        Notification notification;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            init();
            notification = new Notification
                    .Builder(Control.getApplicationContext(),
                    notificationId)
                    .setContentTitle(title)
                    .setContentText(text)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setAutoCancel(false)
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .build();
        } else {
            notification = new Notification.Builder(Control.getApplicationContext())
                    .setContentTitle(title)
                    .setContentText(text)
                    .setAutoCancel(false)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
        }
        try {
            Intent intent = new Intent(Control.getApplicationContext(),
                    Class.forName(className));
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            notification.contentIntent = PendingIntent.getActivity(
                    Control.getApplicationContext(), 0,
                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 在通知栏常驻
        notification.flags = Notification.FLAG_ONGOING_EVENT;
        return notification;
    }

    /**
     * 发送通知
     *
     * @param id           通知ID
     * @param notification 要显示的通知
     */
    public void showNotification(int id, Notification notification) {
        if (mNotificationManager != null) {
            mNotificationManager.notify(id, notification);
        }
    }

    public Notification createNormalNotification(String className, NotificationBean bean) {
        Notification notification;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            notification = new Notification
                    .Builder(Control.getApplicationContext(),
                    notificationId)
                    .setContentTitle(getNormalTitle(bean))
                    .setContentText("查看详细信息")
                    .setTicker(getNormalTitle(bean))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setAutoCancel(true)
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .build();
        } else {
            notification = new Notification.Builder(Control.getApplicationContext())
                    .setContentTitle(getNormalTitle(bean))
                    .setContentText("查看详细信息")
                    .setTicker(getNormalTitle(bean))
                    .setAutoCancel(true)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
        }
        if (bean.getType() != 4) {
            try {
                Intent intent = new Intent(Control.getApplicationContext(),
                        Class.forName(className));
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                intent.putExtra(NOTIFICATION_NAME, bean);
                notification.contentIntent = PendingIntent.getActivity(
                        Control.getApplicationContext(), bean.getTargetId(),
                        intent, PendingIntent.FLAG_UPDATE_CURRENT);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return notification;
    }

    public Notification createFieldWorkNotification(NotificationBean bean) {
        Notification notification;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            notification = new Notification
                    .Builder(Control.getApplicationContext(),
                    notificationId)
                    .setContentTitle("外勤通知")
                    .setContentText(bean.getContext())
                    .setTicker("外勤通知")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setAutoCancel(true)
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .build();
        } else {
            notification = new Notification.Builder(Control.getApplicationContext())
                    .setContentTitle("外勤通知")
                    .setContentText(bean.getContext())
                    .setTicker("外勤通知")
                    .setAutoCancel(true)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
        }
        return notification;
    }

    private String getNormalTitle(NotificationBean bean) {
        String title;
        switch (bean.getType()) {
            case 1:
                title = "请假申请通知";
                break;
            case 2:
                title = "请假申请审批回复";
                break;
            case 3:
                title = "平台通知";
                break;
            default:
                title = "外勤通知";
                break;
        }
        return title;
    }
}
